<?php
namespace Admin\Controller;

use Util\AccessController;

require_once(THINK_PATH . "Library/Util/WxSendMsg/SendMsg.php");

class WxSendMsgController extends AccessController {

    public function index() {
        $wechat = new \Util\WechatTicket();
        $wechat->set(C('WX_APPID'), C('WX_SECRET'));
        $UserData = $wechat->GetAllOpenid();
        $this->count = $UserData['count'];
        $this->UserJosn = json_encode($UserData['data']['openid']);
        $this->display();
    }

    public function KTDATA() {

    }

    //微信消息模板
    //开团通知
    public function KT_SendMsg() {
        $APPID = C('WX_APPID'); //微信公共号appid
        $SECRETID = C('WX_SECRET');//微信公共号 SECRETID
        $template_id = 'FfyxLpos6ztTJfp7Kflzvdww_S_WwGNZFy_qkO3REZw';//消息模板id
        $user = C('ADMIN_OPENID');
        //消息体信息
        $first = $_POST['TITLE'];
        $keyword1 = '尊敬的奇妙阅用户';
        $keyword2 = $_POST['BOOK_NAME'];
        $keyword3 = $_POST['TIME'];
        $keyword4 = $_POST['Address'];
        $remark = $_POST['DB_DATA'];
        $url = $_POST['URL'];//链接
        $num = 0;
        foreach ($user as $k => $v) {//发送消息模板对象用户openid
            $Send = new \Util\WxSendMsg\SendMsg();
            $data = array($keyword1, $keyword2, $keyword3, $keyword4);
            $Send->init($APPID, $SECRETID, $v, $template_id, $url);
            $Send->Setfirst($first);
            $Send->Setremark($remark);
            $Send->Setdata($data);
            $OK = $Send->SendMsg();
            if ($OK == 0) {
                $num++;
            }
        }
        $this->success('发送成功通知条数：' . $num);

    }

    //微信消息模板
    //开团通知
    public function KT_ALL_SendMsg() {
        $APPID = 'wx054896dce50a2918'; //微信公共号appid
        $SECRETID = '60532758cd2cc25c41eb1b0642f4d043';//微信公共号 SECRETID
        $template_id = 'FfyxLpos6ztTJfp7Kflzvdww_S_WwGNZFy_qkO3REZw';//消息模板id
        $OPENID = $_POST['OPENID'];

        //消息体信息
        $first = $_POST['TITLE'];
        $keyword1 = '尊敬的奇妙阅用户';
        $keyword2 = $_POST['BOOK_NAME'];
        $keyword3 = $_POST['TIME'];
        $keyword4 = $_POST['Address'];
        $remark = $_POST['DB_DATA'];
        $url = $_POST['URL'];//链接
        $Send = new \Util\WxSendMsg\SendMsg();
        $data = array($keyword1, $keyword2, $keyword3, $keyword4);
        $Send->init($APPID, $SECRETID, $OPENID, $template_id, $url);
        $Send->Setfirst($first);
        $Send->Setremark($remark);
        $Send->Setdata($data);
        $OK = $Send->SendMsg();
        if ($OK) {
            echo 1;
        } else {
            echo 0;
        }

    }

    /*
     * 用户获得优惠劵通知
     *
     */
    public function YHJ_SendMsg($OPENID, $CARD_NAME) {
        $APPID = C('WX_APPID'); //微信公共号appid
        $SECRETID = C('WX_SECRET');//微信公共号 SECRETID
        $template_id = 'N01WlrpVCwCV20RCyYKQMbATbDZ98fFCwdRqYcpDmCA';//消息模板id
        //消息体信息
        $first = '您在奇妙阅平台获得一张优惠券';
        $keyword1 = '尊敬的奇妙阅平台用户';
        $keyword2 = $CARD_NAME;

        $keyword3 = date('Y-m-d H:i:s');
        $remark = '低价好书数量有限，戳我去用券吧o(∩_∩)o ';
        $url = 'http://www.yanxiang.pw/coupons?isSelect=false';//链接
        $Send = new \Util\WxSendMsg\SendMsg();
        $data = array($keyword1, $keyword2, $keyword3);
        $Send->init($APPID, $SECRETID, $OPENID, $template_id, $url);
        $Send->Setfirst($first);
        $Send->Setremark($remark);
        $Send->Setdata($data);
        $OK = $Send->SendMsg();

    }


    /*
     * 出库人员通知
     * order_id 订单id
     */
    public function CKOrder_SendMsg($order_id) {
        $LS = Service('M','order');
        $where['order_id'] = $order_id;
        $Order =  $LS->find($where);
        $APPID = C('WX_APPID'); //微信公共号appid
        $SECRETID = C('WX_SECRET');//微信公共号 SECRETID
        $template_id = 'lXm-BetKbFLVkjeekr0ulWSKoxx1WJcl-yj2ZuHsENs';//消息模板id
        $user = C('ADMIN_OPENID');
        //消息体信息
        $first = '客户订单所有书籍已经出库成功啦，快去仓库取书帮TA发快递';
        $keyword1 = '共'.$Order['order_num'].'本书';
        $keyword2 = $Order['order_book_money'];
        $keyword3 = $Order['order_address_name'].' '. $Order['order_phone'];
        $keyword4 = $Order['order_address'];
        $keyword5 = $Order['order_start_time'];
        $remark = '-->点击查看详情吧o(∩_∩)o ';
        $url =  'http://www.yanxiang.pw/orders/admin?order_id=' . $order_id;//链接
        $Send = new \Util\WxSendMsg\SendMsg();
        $data = array($keyword1, $keyword2,$keyword3,$keyword4,$keyword5);
        foreach ($user as $k => $v) {
            $Send->init($APPID, $SECRETID, $v, $template_id, $url);
            $Send->Setfirst($first);
            $Send->Setremark($remark);
            $Send->Setdata($data);
            $OK = $Send->SendMsg();
        }
    }

    /*
     * 到货通知
     */
    public function Book_TZ_SendMsg($BOOK_ID, $BOOK_NAME, $OPENID) {
        $APPID = C('WX_APPID'); //微信公共号appid
        $SECRETID = C('WX_SECRET');//微信公共号 SECRETID
        $template_id = 'zCiHZLLeMKxXooZMHshawbGu9WLXOU8LgRoHFXkgHFo';//消息模板id
        $user = $OPENID;
        //消息体信息
        $first = '亲，您心心念念的那本书到货啦。';
        $keyword1 = $BOOK_NAME;
        $keyword2 = '1本';
        $remark = '温馨提醒：低价好书数量有限，快去下单吧，免得又被抢光哦!
点击“详情”查看书籍';
        $url = 'http://www.yanxiang.pw/book/' . $BOOK_ID;//链接
        $Send = new \Util\WxSendMsg\SendMsg();
        $data = array($keyword1, $keyword2);
        $Send->init($APPID, $SECRETID, $user, $template_id, $url);
        $Send->Setfirst($first);
        $Send->Setremark($remark);
        $Send->Setdata($data);
        $OK = $Send->SendMsg();
        echo $OK;
    }

    /*
     * 回收打款通知
     */
    public function GetOrder_DK_Send_Old($OPENID,$ORDER_ID,$MONEY){
        $APPID = C('WX_APPID'); //微信公共号appid
        $SECRETID = C('WX_SECRET');//微信公共号 SECRETID
        $template_id = 'x569_nVdRHZKYHQg8yBPnPcv3hIcipQNf51LhLRPLKY';//消息模板id
        $user = $OPENID;
        //消息体信息
        $first = '亲，您在奇妙阅卖书赚的钱已打入您的账户啦！';
        $keyword1 = $ORDER_ID;
        $keyword2 = $MONEY;
        $remark = '点击“详情”查看账户余额，如有问题请在奇妙阅公众号后台留言。';
        $url = 'http://www.yanxiang.pw/account';//链接
        $Send = new \Util\WxSendMsg\SendMsg();
        $data = array($keyword1, $keyword2);
        $Send->init($APPID, $SECRETID, $user, $template_id, $url);
        $Send->Setfirst($first);
        $Send->Setremark($remark);
        $Send->Setdata1($data);
        dump($Send);
        $OK = $Send->SendMsg();
        echo $OK;
    }


    /*
 * 回收打款通知
 */
    public function GetOrder_DK_Send($OPENID,$ORDER_ID,$MONEY){
        $APPID = C('WX_APPID'); //微信公共号appid
        $SECRETID = C('WX_SECRET');//微信公共号 SECRETID
        $template_id = 'zVBvKGZEr3suf-Y7MHwdom9nQtmBAeLbPjNT7mbAysk';//消息模板id
        $user = $OPENID;
        //消息体信息
        $first = '亲，您在奇妙阅卖书赚的钱已打入您的账户啦！可提现，也可在平台直接买书使用哦。';
        $keyword1 = '奇妙阅卖书';
        $keyword2 = $MONEY;
        $keyword3 = date('Y-m-d H:i:s');
        $remark = '点击“详情”查看账户余额，如有问题请在奇妙阅公众号后台留言。';
        $url = 'http://www.yanxiang.pw/account';//链接
        $Send = new \Util\WxSendMsg\SendMsg();
        $data = array($keyword1, $keyword2,$keyword3);
        $Send->init($APPID, $SECRETID, $user, $template_id, $url);
        $Send->Setfirst($first);
        $Send->Setremark($remark);
        $Send->Setdata($data);
        $OK = $Send->SendMsg();
        echo $OK;
    }

    /*
     * 收书帮用户呼叫快递后通知用户
     */
    public function GetOrder_HJKD_Send($OPENID,$SM_TIME,$address,$order_od){
        $APPID = C('WX_APPID'); //微信公共号appid
        $SECRETID = C('WX_SECRET');//微信公共号 SECRETID
        $template_id = '0EIX-nH5sLtrGbqk4nVr4pA-CQCyBU3Iv6L-fwsXL6A';//消息模板id
        $user = $OPENID;
        //消息体信息
        $first = '亲，您的卖书订单我们已经收到，顺丰小哥已经整装待发，时间一到就会出现在您面前取件啦。';
        $keyword1 = $SM_TIME;
        $keyword2 = $address;
        $remark = '温馨提醒：请准备好要卖的书籍，并保持电话通畅哦！
点击“详情”查看卖书订单';
        $url = 'http://www.yanxiang.pw/sell/info?oid='.$order_od;//链接
        $Send = new \Util\WxSendMsg\SendMsg();
        $data = array($keyword1, $keyword2);
        $Send->init($APPID, $SECRETID, $user, $template_id, $url);
        $Send->Setfirst($first);
        $Send->Setremark($remark);
        $Send->Setdata($data);
        $OK = $Send->SendMsg();
    }

    /*
     * 用户下单买书发货通知
     */
    public function Order_FH_Send($OPENID,$order_code,$order_money,$order_num,$order_address,$order_address_tilte,$order_id){
        $APPID = C('WX_APPID'); //微信公共号appid
        $SECRETID = C('WX_SECRET');//微信公共号 SECRETID
        $template_id = 'SkSYpaCt2rF2VE1R3b_jx0HzyjF41ra82FLs_bJZ4dU';//消息模板id
        $user = $OPENID;
        //消息体信息
        $first = '亲爱的书友，您的订单已经发货，请留意查收。';
        $keyword1 = $order_code;
        $keyword2 = $order_money;
        $keyword3 = '书籍本数:'.$order_num.'本';
        $keyword4 = $order_address;
        $keyword5 = $order_address_tilte;
        $remark = '温馨提醒：保持电话通畅哦！
点击“详情”查看订单';
        $url =  'http://www.yanxiang.pw/orders/info?order_id=' . $order_id;//链接
        $Send = new \Util\WxSendMsg\SendMsg();
        $data = array($keyword1, $keyword2,$keyword3,$keyword4,$keyword5);
        $Send->init($APPID, $SECRETID, $user, $template_id, $url);
        $Send->Setfirst($first);
        $Send->Setremark($remark);
        $Send->Setdata($data);
        $Send->SendMsg();
    }
    /*
     * 书籍未通过审核
     */
    public function NotHsOrderBook_Send($OPENID,$order_id,$first_str,$order_type){
        $APPID = C('WX_APPID'); //微信公共号appid
        $SECRETID = C('WX_SECRET');//微信公共号 SECRETID
        $template_id = 'KQ51ZAlO2gAxtzS1gSIbzrb2gYHuTxzwqhFF-MDoy3c';//消息模板id
        $user = $OPENID;
        //消息体信息
        $first = $first_str;
        $keyword1 = $order_id;
        $keyword2 =$order_type;
        $remark = '点击查看订单详情。';
        $url =  'http://www.yanxiang.pw/sell/info?oid=' . $order_id;//链接
        $Send = new \Util\WxSendMsg\SendMsg();
        $data = array($keyword1, $keyword2);
        $Send->init($APPID, $SECRETID, $user, $template_id, $url);
        $Send->Setfirst($first);
        $Send->Setremark($remark);
        $Send->Setdata2($data);
        $Send->SendMsg();
    }

    public function test() {
        $Send = A('Admin/WxSendMsg');
        $DATA = $Send->NotHsOrderBook_Send('on7J8046NthBty6rQttBD-uQUSwg','WXHSOID_5ab870269c9a831');
        dump($DATA);

    }

}